package eventtype

import (
	"encoding/json"
	"iot-base/common/propertytype"
	"time"

	"gorm.io/datatypes"
)

type Datetime time.Time

func (t Datetime) MarshalJSON() ([]byte, error) {
	return json.Marshal(time.Time(t).Format("2006-01-02 15:04:05"))
}

//Event 事件
type Event struct {
	UID         int64                  `gorm:"primaryKey;AUTO_INCREMENT;not null" json:"uid"`
	TID         string                 `gorm:"uniqueIndex:event_index" json:"t_id"`
	EventID     string                 `gorm:"uniqueIndex:event_index" json:"event_id"`
	Time        time.Time              `gorm:"uniqueIndex:event_index" json:"time"`
	Level       int64                  `json:"level"`
	Properties  []propertytype.Value   `gorm:"-" json:"properties"`
	Params      map[string]interface{} `gorm:"-" json:"params"`
	JProperties datatypes.JSON         `json:"-" `
	JParams     datatypes.JSON         `json:"-"`
	StationName string                 `json:"station_name"`
	EventName   string                 `json:"event_name"`
	StationID   string                 `json:"station_id"`
	IsNotSaveDB bool                   `json:"is_not_save_db"`
}

//ToDBObj 转换为数据库对象存储
func (event *Event) ToDBObj() {
	if event.Params != nil {
		event.JProperties, _ = json.Marshal(event.Properties)
		event.JParams, _ = json.Marshal(event.Params)
	}
}

//ToDBObj 转换为网络对象发送
func (event *Event) ToNetObj() {
	if event.Params != nil {
		if event.JParams != nil {
			var params map[string]interface{}
			json.Unmarshal(event.JParams, &params)
			event.Params = params
		}
		if event.JProperties != nil {
			var ps []propertytype.Value
			json.Unmarshal(event.JProperties, &ps)
			event.Properties = ps
		}
	}
}

//AlarmKey 告警key
type AlarmKey struct {
	TID string `gorm:"uniqueIndex:alarm_index" json:"t_id"`
	//PID     string `gorm:"uniqueIndex:alarm_index" json:"m_id"`
	//SubID   string `gorm:"uniqueIndex:alarm_index" json:"sub_id"`
	Mindex  int64  `gorm:"uniqueIndex:alarm_index" json:"mindex"`
	AlarmID string `gorm:"uniqueIndex:alarm_index" json:"alarm_id"`
}

//Info 信息
type Info struct {
	Properties []propertytype.Value   `json:"properties"`
	Params     map[string]interface{} `json:"params"`
	Checker    string                 `json:"checker"`
}

//AlarmBase 告警
type AlarmBase struct {
	AlarmKey
	StationID   string `json:"station_id"`
	StationName string `json:"station_name"`
	Time        int64  `json:"time"`
	CatalogID   string `json:"catalog_id"`
	Info
	IsHide bool `json:"is_hide"`
}

type AlarmDisp struct {
	ThingName string `json:"thing_name"`
	AlarmName string `json:"alarm_name"`
	MName     string `json:"m_name"` //物模型名称
	SubName   string `json:"sub_name"`
	Level     int64  `json:"level"`
}

//Alarm 告警
type Alarm struct {
	Flag string `json:"flag"`
	AlarmBase
	ClearTime int64 `json:"clear_time"`
	AlarmDisp
}

//AlarmClear 接入库构建该数据结构
type AlarmClear struct {
	UID int64 `gorm:"primaryKey;AUTO_INCREMENT;not null" json:"uid"`
	AlarmKey
	ClearTime time.Time `json:"clear_time"`
}

//AlarmChanged 告警变化
type AlarmChanged struct {
	IsAlarm     bool     `json:"alarm_state"`
	TimeChanged Datetime `json:"time_changed"`
}

//AlarmDigist 告警摘要 redis中，后续版本放弃使用该结构与AlarmState合并
type AlarmDigist struct {
	//AlarmBase
	IsAlarm       bool   `json:"alarm_state" redis:"IsAlarm"`
	TimeChanged   int64  `json:"time_changed" redis:"TimeChanged"`
	TimeRefreshed int64  `json:"time_refreshed" redis:"TimeRefreshed"`
	Level         int64  `json:"level" redis:"Level"`
	Delay         int64  `json:"delay" redis:"Delay"`
	JInfo         string `json:"-"  redis:"JInfo"`
	Info          *Info  `json:"info"`

	//DelayLimit int64 `json:"delay_limit"`
}

//AlarmState 告警视图 数据库中
type AlarmState struct {
	UID int64 `gorm:"primaryKey;AUTO_INCREMENT;not null" json:"uid"`
	AlarmKey
	StationID   string `gorm:"default:'';type:varchar(40);index" json:"station_id"`
	StationName string `gorm:"default:'';type:varchar(100)" json:"station_name"`
	AlarmChanged
	AlarmObj datatypes.JSON `json:"alarm_obj"`
	AlarmDisp
	Delay         int64  `json:"delay"`
	CatalogID     string `gorm:"default:'';type:varchar(40)"json:"catalog_id"`
	RefreshedTime int64
	IsHide        bool `json:"is_hide"`
	//Info
}

//AlarmHistroy 告警历史表，存储已经消除的告警
type AlarmHistroy struct {
	UID int64 `gorm:"primaryKey;AUTO_INCREMENT;not null" json:"uid"`
	Seq int64
	AlarmKey
	StationID   string         `gorm:"default:'';type:varchar(40);index" json:"station_id"`
	StationName string         `gorm:"default:'';type:varchar(100)" json:"station_name"`
	CatalogID   string         `gorm:"default:'';type:varchar(40)"json:"catalog_id"`
	AlarmTime   Datetime       `json:"alarm_time"`
	ClearTime   Datetime       `json:"clear_time"`
	AlarmObj    datatypes.JSON `json:"alarm_obj"`
	ClearObj    datatypes.JSON `json:"clear_obj"`
	AlarmDisp
	IsHide bool `json:"is_hide"`
}

//CreateAlarm 创建告警
func CreateAlarm(tid, alarmId string, aindex int64, flag bool, level int64) (alarm Alarm) {
	alarm.TID = tid
	alarm.AlarmID = alarmId
	alarm.Mindex = aindex
	if flag {
		alarm.Flag = "BEGIN"
	} else {
		alarm.Flag = "END"
	}
	alarm.Level = level

	return alarm
}
